#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QDebug>
#include "register1.h"
#include "ui_register1.h"
#include "forget_1.h"
#include "ui_forget_1.h"
#include "chat1.h"
#include "ui_chat1.h"

login::login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::login)
{
    ui->setupUi(this);
    socket = new QTcpSocket(this);

    connect(socket, &QTcpSocket::connected, this, &login::onConnected);
    connect(socket, &QTcpSocket::readyRead, this, &login::onReadyRead);
    connect(socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &login::onError);

    socket->connectToHost("192.168.159.131", 1234);

    QLineEdit *lineEdit = ui->mima2;
    if (lineEdit) {
        lineEdit->setEchoMode(QLineEdit::Password);
    }

}

login::~login()
{
    delete ui;
}

void login::on_zhuce_clicked() {
    register1 *newWidget = new register1();  // 创建新窗口对象
    newWidget->show();  // 显示新窗口
    this->close();  // 关闭当前窗口
}

void login::on_denglu_clicked() {
    QString username = ui->yonghuming2->text();
    QString password = ui->mima2->text();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "提示", "用户名，密码不能为空");
    } else {
        // 向服务器发送登录信息
        QString message = "LOGIN\n";
        message += username + "\n" + password + "\n";
        socket->write(message.toUtf8());

        qDebug() << "发送信息";
    }

}

void login::on_forget_clicked()
{
    QString username = ui->yonghuming2->text();

    if (username.isEmpty()) {
        QMessageBox::warning(this, "提示", "用户名不能为空");
    } else {
        QString message = "PROBLEM\n";
        message += username + "\n";
        socket->write(message.toUtf8());

        qDebug() << "发送信息";
    }

}

void login::onConnected() {
    qDebug() << "已连接到服务器！";
}

void login::onReadyRead() {
    QByteArray data = socket->readAll();
    QDataStream stream(&data, QIODevice::ReadOnly);
    QString message;
    stream >> message;

    int spaceIndex = message.indexOf('\n'); // 寻找第一个空格的位置

    QString responseType = message.left(spaceIndex);
    QString Data = message.mid(spaceIndex + 1);

    if (responseType == "LOGIN_SUCCESS") {
        QString username = ui->yonghuming2->text();
        // 登录成功，进入新界面
        chat1 *newWidget = new chat1(username);  // 创建新窗口对象
        newWidget->show();  // 显示新窗口
        newWidget->setWindowTitle(username);
        this->close();  // 关闭当前窗口
    } else if (responseType == "LOGIN_FAIL") {
        QMessageBox::warning(this, "提示", "登录失败");
    } else if (responseType == "PROBLEM_SUCCESS") {
        QString username = ui->yonghuming2->text();
        forget_1 *newWidget = new forget_1(username,Data);  // 创建新窗口对象
        newWidget->show();  // 显示新窗口
        this->close();  // 关闭当前窗口
    } else if (responseType == "PROBLEM_FAIL") {
        QMessageBox::warning(this, "提示", "请输入正确用户名");
    }
}

void login::onError(QAbstractSocket::SocketError socketError) {
    qDebug() << "Socket错误:" << socketError;
}

void login::on_tuichu_clicked()
{
    this->close();
}
